import tagProcess from "./tagProcess.json";

const getProcess = (scheme = "NN") => {
  let output = {};
  for (let i = 0; i < tagProcess.length; i++) {
    output[tagProcess[i].TAG] = tagProcess[i][scheme];
  }
  return output;
};

let shortPoints = {};

const matchLinks = (data, process, scheme) => {
  let links = {};
  shortPoints = {};
  for (let i = 0; i < data.length; i++) {
    let tag = data[i].tag;
    let tagComponents = tag.split("_");
    let tagAirport = tagComponents.length == 3 ? tagComponents[0] : "OTHER";
    let tagPoint = tagComponents.length == 3 ? tagComponents[2] : "OTHER";
    let tagType = tagComponents.length == 3 ? tagComponents[1] : "OTHER";
    let value = data[i].val;
    if (!shortPoints[tagAirport]) {
      shortPoints[tagAirport] = {
        name: tagAirport,
        value: value,
        type: "airport",
        type2: tagType,
      };
    } else {
      shortPoints[tagAirport].value += value;
    }
    if (!shortPoints[tagPoint]) {
      shortPoints[tagPoint] = {
        name: tagPoint,
        value: value,
        type: "point",
        type2: tagType,
      };
    } else {
      shortPoints[tagPoint].value += value;
    }
    if (process[tag]) {
      let sequence = process[tag].replace(/;$/gi, "").split(";");
      // let sequence = process[tag].split(";").map((cell) => cell.split(",")[0]);
      if (tagType == "DEP") {
        sequence.reverse();
      }
      for (let j = 0; j < sequence.length; j++) {
        let sequenceComponents = sequence[j].split(",");
        let sector = sequenceComponents[0];
        let ratio = sequenceComponents[1];
        let seat = findSeat(sector, scheme, {
          tag,
          seq: sequence.join(";"),
          j,
        });
        if (j == 0) {
          let label = `${tagPoint}-${seat}~${tagAirport}`;
          if (!links[label]) {
            links[label] = {
              source: tagPoint,
              target: seat,
              value: 0,
              value2: 0,
              type: tagType,
              airport: tagAirport,
              tags: [],
            };
          }
          if (!links[label].tags.includes(tag)) {
            links[label].tags.push(tag);
            links[label].value += value;
            links[label].value2 += value * ratio;
          }
        } else if (j == sequence.length - 1) {
          let label = `${seat}-${tagAirport}~${tagAirport}`;
          if (!links[label]) {
            links[label] = {
              source: seat,
              target: tagAirport,
              value: 0,
              value2: 0,
              type: tagType,
              airport: tagAirport,
              tags: [],
            };
          }
          if (!links[label].tags.includes(tag)) {
            links[label].tags.push(tag);
            links[label].value += value;
            links[label].value2 += value * ratio;
          }
        }
        if (j > 0) {
          let seatPrev = findSeat(sequence[j - 1].split(",")[0], scheme, {
            tag,
            seq: sequence.join(";"),
            j: j - 1,
          });
          if (seatPrev != seat) {
            let label = `${seatPrev}-${seat}~${tagAirport}`;
            if (!links[label]) {
              links[label] = {
                source: seatPrev,
                target: seat,
                value: 0,
                value2: 0,
                type: tagType,
                airport: tagAirport,
                tags: [],
              };
            }
            if (!links[label].tags.includes(tag)) {
              links[label].tags.push(tag);
              links[label].value += value;
              links[label].value2 += value * ratio;
            }
          }
        }
      }
    }
  }
  return links;
};

const findSeat = (sectorCode, sectorScheme, ex = null) => {
  for (let i = 0; i < sectorScheme.length; i++) {
    if (sectorScheme[i].sectorCodes.includes(sectorCode)) {
      return sectorScheme[i].seatName;
    }
  }
  if (ex) {
    console.log("findSeatError", sectorCode, ex);
  }
  return sectorCode;
};

const matchSectors = (data, process = getProcess()) => {
  let output = new Array();
  for (let i = 0; i < data.length; i++) {
    let tag = data[i].tag;
    let value = data[i].val;
    if (process[tag]) {
      let sectors = new Array();
      let tmp = process[tag].split(";");
      tmp.forEach((t, i) => {
        if (t != "") {
          let tmpa = t.split(",");
          sectors.push({ code: tmpa[0], ratio: tmpa[1] });
        }
      });
      output.push({ value, sectors });
    }
  }
  return output;
};

const matchSeat = (data, scheme) => {
  // each seat
  let output = new Array();
  for (let i = 0; i < scheme.length; i++) {
    let sectors = scheme[i].sectorCodes;
    if (sectors.length == 0) continue;
    let seat = scheme[i].seatName;
    let value = 0;
    let load = 0;
    data.forEach((cell) => {
      let cellValue = 0;
      let cellRatio = 0;
      cell.sectors.forEach((cellSector) => {
        if (sectors.includes(cellSector.code)) {
          cellValue = cell.value;
          if (cellRatio < cellSector.ratio) {
            cellRatio = cellSector.ratio;
          }
        }
      });
      if (cellValue > 0) {
        value += cellValue;
        load += cellValue * cellRatio;
      }
    });
    load = Math.round(load);
    output.push({ name: seat, value: value, value2: load, sectors: sectors });
  }
  return output;
};

export default function (taggedCell, directionScheme, sectorScheme) {
  let verbose = 1;
  if (verbose)
    console.log("function: arrangeSectorData", {
      taggedCell,
      directionScheme,
      sectorScheme,
    });
  if (!taggedCell || !sectorScheme) return null;
  let process = getProcess(directionScheme);
  let sectorData = matchSectors(taggedCell, process);
  let nodeData = matchSeat(sectorData, sectorScheme);
  let linkData = matchLinks(taggedCell, process, sectorScheme);
  if (verbose)
    console.log("function: arrangeSectorDataResult", {
      sectorData,
      // seatData,
      nodeData,
      linkData,
      shortPoints,
    });
  return { nodeData, linkData, shortPoints };
}
